package com.wzgl.core.utils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jfinal.plugin.activerecord.Model;
  
public class JsonToMapUtil {  
  
    /** 
     * model转换为map 
     *  
     * @param model 
     * @return 
     * @author: Jerri Liu 
     * @date: 2014年3月30日下午5:17:33 
     */  
    public static Map<String, Object> JsonToMap2(Model<?> model) {  
        String jmodel = model.toJson();  
        Gson gson = new Gson();  
        Map<String, Object> jsonmap = gson.fromJson(jmodel,  
                new TypeToken<Map<String, Object>>() {  
                }.getType());  
        return jsonmap;  
    }  
  
    /** 
     * 将json对象转换成Map 
     *  
     * @param jsonmap 
     * @return 
     * @author: Jerri Liu 
     * @date: 2014年3月30日下午5:17:33 
     */  
    @SuppressWarnings("unchecked")  
    public static Map<String, Object> jsonToMap(JSONObject jsonmap) {  
        Map<String, Object> map = new HashMap<String, Object>();  
        Iterator<String> iterator = (Iterator<String>) jsonmap.keys();  
        String key = null;  
        Object value = null;  
        while (iterator.hasNext()) {  
            key = iterator.next();  
            try {  
                value = jsonmap.get(key);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            map.put(key, value);  
        }  
        return map;  
    }  
    /**
     * 从request中获得参数Map，并返回可读的Map
     * 
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map getParameterMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
    /**
     * 从request中获得参数Map，并返回可读的Map
     * 
     * @param request
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map getParameterMap(Map<String, String[]> properties) {
        // 参数Map
//        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if(null == valueObj){
                value = "";
            }else if(valueObj instanceof String[]){
                String[] values = (String[])valueObj;
                for(int i=0;i<values.length;i++){
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length()-1);
            }else{
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
}  